package action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_news.EntityBeanOc_News;
import entitybean.oc_study.EntityBeanOc_Study;
import form.Insert_StudyForm;
import form.NewsListForm;
import form.StudyListForm;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.Convert;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_News;
import view.ViewOc_Study;

public class Action_Oc_Study extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;

	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}

	public ActionForward admin_init_study(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StudyListForm newslistform = (StudyListForm) form;
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_STUDY where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Study> list = interfaceforresultsettolist
				.ViewOc_Study(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_load_study(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		System.out.println("ID=" + id);
		EntityBeanOc_Study e = this.service_admin.Find_Oc_Study_By_Id(id);
		String rootPath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		System.out.println(e.getId());
		Convert c = new Convert();
		e.setNews_clob_string(c.ConvertClobToString(e.getNews_clob()));
		request.setAttribute("e", e);
		// response.sendRedirect(rootPath +
		// "/kindedit/jsp/ocean_demo_update_news.jsp?title="+e.getNews_title());
		return mapping.findForward("success");
	}

	public ActionForward admin_add_study(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String rootPath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		// response.sendRedirect(rootPath +
		// "/kindedit/jsp/ocean_demo_add_news.jsp");

		// return null;
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_study(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		StudyListForm fileUploadForm = (StudyListForm) form;		
		FormFile  file = fileUploadForm.getFile();  	
		String fileName="";
		  if(!"".equals(file.getFileName())&&file.getFileName()!=null){
           java.util.Date date= new java.util.Date();      
           fileName =date.getTime() + file.getFileName().substring( file.getFileName().lastIndexOf("."));               
           String pathName = request.getRealPath("/")+ "/upload/" + fileName;    
          try {     
             FileOutputStream os = new FileOutputStream(new File(pathName));       
             os.write(file.getFileData()); 
             os.close();                 
           } catch (FileNotFoundException e1) { 
             e1.printStackTrace();         
           }
		  }
		String news_clobstring =fileUploadForm.getNews_clob();
	//	String news_clobstring1 = request.getParameter("news_clob");
		String news_title = fileUploadForm.getNews_title();
		// news_clobstring="news_clobstringnews_clobstringnews_clobstringnews_clobstring";
		EntityBeanOc_Study e = new EntityBeanOc_Study();
		EntityBeanL_Sequence l = this.service_public
				.Find_L_Sequence_By_seq_Name("SEQ_OC_NEWS");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		e.setId(id);
		e.setDel("OFF");
		e.setNews_clob_string(news_clobstring);
		e.setNews_title(news_title);
		e.setUpdate_time(new Date());
		e.setUser_id(1);
		e.setPath(fileName);
		this.service_admin.Insert_Oc_Study(e);
		this.service_public.Update_L_Sequence(l);

		StudyListForm newslistform = (StudyListForm) form;
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_STUDY where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Study> list = interfaceforresultsettolist
				.ViewOc_Study(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 璁剧疆浼犻�掑埌椤甸潰鐨勬暟鎹�
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_delete_study(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		StudyListForm insert_newsform=(StudyListForm)form;
		String id=insert_newsform.getId();
		System.out.println("id=" + id);
		id = this.cut_tail(id);
		this.service_admin.Delete_Oc_Study(id);
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_STUDY where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(insert_newsform.getRowcountperpage()), Integer.valueOf(insert_newsform
						.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Study> list = interfaceforresultsettolist
				.ViewOc_Study(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", insert_newsform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_update_study(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StudyListForm insert_newsform=(StudyListForm)form;
		FormFile  file = insert_newsform.getFile();  
		String id=insert_newsform.getId();
		String title=insert_newsform.getNews_title();
		String clob=insert_newsform.getNews_clob();
		String path=insert_newsform.getPath();
		EntityBeanOc_Study e=new EntityBeanOc_Study();
		String fileName="";
		if(!"".equals(file.getFileName())&&file.getFileName()!=null){
	    java.util.Date date= new java.util.Date();      
	    fileName =date.getTime() + file.getFileName().substring( file.getFileName().lastIndexOf("."));               
	    String pathName = request.getRealPath("/")+ "/upload/" + fileName; 
	       try {     
	          FileOutputStream os = new FileOutputStream(new File(pathName));       
	          os.write(file.getFileData()); 
	          os.close();                 
	         } catch (FileNotFoundException e1) { 
	          e1.printStackTrace();         
	         }
		}
		e.setId(Integer.valueOf(id));
		e.setNews_title(title);
		e.setNews_clob_string(clob);
		e.setUpdate_time(new Date());
		e.setUser_id(1);
		if(fileName==""){
			e.setPath(path);
		}else{
			e.setPath(fileName);	
		}
		e.setDel("OFF");
		this.service_admin.Delete_Oc_Study(e);
		this.service_admin.Insert_Oc_Study(e);
		StudyListForm newslistform = new StudyListForm();
		newslistform.setCurrentpage("1");
		newslistform.setNews_title("");
		newslistform.setOver_time("");
		newslistform.setStart_time("");
		String sql = "select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_STUDY where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer
						.valueOf(newslistform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Study> list = interfaceforresultsettolist
				.ViewOc_Study(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
		
		
		
		
	}

	public ActionForward admin_search_study(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception  
	{
		StudyListForm newslistform=(StudyListForm)form;
		String sql="select NEWS_TITLE,UPDATE_TIME,USER_ID,ID from OC_STUDY where del='OFF' ORDER BY UPDATE_TIME DESC";
		PageBeanPlus pagebeanplus=new PageBeanPlus("oracle", "sql",sql,
				Integer.valueOf(newslistform.getRowcountperpage()), Integer.valueOf(newslistform.getCurrentpage()),"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist=new InterfaceForResultSetToList();
		List<ViewOc_Study>list=interfaceforresultsettolist.ViewOc_Study(pagebeanplus.getFormatqueryresultset());
		
		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", newslistform);
		System.out.println("list.size()"+list.size());
		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
